import Vue from 'vue'
import VueRouter from 'vue-router'

import store from '@/store/index'

import { Toast } from 'vant'

import LayoutHome from '@/views/layout.vue'
import Home from '@/views/index/home.vue'
import Classify from '@/views/index/classify.vue'
import Cart from '@/views/index/cart.vue'
import Store from '@/views/index/store.vue'
import User from '../views/index/user.vue'

import Reset from '@/views/index/login/reset'
import Register from '@/views/index/login/register'
import Coupon from '@/views/member/coupon'

import Layout from '@/views/layout-'
import Invoice from '@/views/member/invoice'
import AddInvoice from '@/views/member/add-invoice'
import Grade from '@/views/member/grade'
import CellectShop from '@/views/member/collect/shop'
import CellectGoods from '@/views/member/collect/goods'
import Footprints from '@/views/member/collect/footprints'
import Order from '@/views/member/order'
import Logistic from '@/views/member/logistic'
import Shop from '@/views/shop/index'
import Address from '@/views/member/address'
import AddressEdit from '@/views/member/address-edit'
import AfterSale from '@/views/member/after-sale'
import Invite from '@/views/member/invite'
import Invites from '@/views/member/invites'
import InviteMember from '@/views/member/invite-member'
import InviteRule from '@/views/member/invite-rule'

import Product from '@/views/goods/details'
import Checkout from '@/views/pay/checkout'
import Pay from '@/views/pay/index'

import Search from '@/views/search/index'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'LayoutHome',
    component: LayoutHome,
    redirect: '/home',
    children: [
      { path: '/home', component: Home, meta: { title: '首页' } },
      { path: '/classify', component: Classify, meta: { title: '分类' } },
      { path: '/cart', component: Cart, meta: { title: '购物车' } },
      { path: '/store', component: Store, meta: { title: '店铺' } },
      { path: '/user', component: User, meta: { title: '我的' } },
      { path: '/reset', component: Reset, meta: { title: '重置密码' } }, // 重置密码
      { path: '/register', component: Register, meta: { title: '注册' } },
      { path: '/coupon', component: Coupon, meta: { title: '优惠卷' } } // 优惠卷
    ]
  },
  {
    path: '/',
    name: 'Layout',
    component: Layout,
    meta: {
      isToken: true,
      title: '我的'
    },
    children: [ 
      { path: '/invoice', component: Invoice }, // 发票
      { path: '/addinvoice', component: AddInvoice },
      { path: '/grade', component: Grade }, // 等级
      { path: '/cellect-shop', component: CellectShop },
      { path: '/cellect-goods', component: CellectGoods },
      { path: '/footprints', component: Footprints }, //足迹
      { path: '/order/:index', component: Order }, // 订单
      { path: '/logistic', component: Logistic }, // 物流
      { path: '/shop', component: Shop }, // 店铺
      { path: '/address', component: Address }, // 地址列表
      { path: '/address-edit', component: AddressEdit }, // 地址新增 编辑
      { path: '/product', component: Product }, // 商品详情
      { path: '/after-sale', component: AfterSale }, // 申请售后 售后详情

      { path: '/checkout', component: Checkout, meta: { isToken: true } }, // 确认订单
      { path: '/pay', component: Pay, meta: { isToken: true, title: '支付订单' } }, // 支付
      
      { path: '/invite', component: Invite, meta: { title: '邀请' } }, // 间接邀请
      { path: '/invites', component: Invites, meta: { title: '邀请' } }, // 邀请
      { path: '/invite-member', component: InviteMember, meta: { title: '邀请' } }, // 邀请
      { path: '/invite-rule', component: InviteRule, meta: { title: '邀请规则' } } // 邀请规则
    ]
  },
  { path: '/search', component: Search } // 搜索页
  // {
  //   path: '/about',
  //   name: 'About',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  // }
]

const router = new VueRouter({
  // mode: 'history',
  routes
})

const setDocumentTitle = (val) => {
  document.title = val
}
router.beforeEach((to, from, next) => {

  // 需要登录 页面
  if (to.meta.isToken && !store.state.user.profile.token) {
    Toast('请先登录')
    // next({ path: '/user', query: { redirectUrl: to.fullPath } })
    return
  }

  if (to.meta.title) {
    setDocumentTitle(to.meta.title)
  }
  next()
})
// router.afterEach(() => {

// })

export default router
